import 'package:flutter/material.dart';
import '../utils/base.dart';
import 'styles.dart';

class XPage extends StatelessWidget {
  final Widget appbar;
  final Widget body;

  const XPage({Key key, this.appbar, this.body}) : super(key: key);

  //style:S
  BoxDecoration _boxDecorationOfAppBar() {
    return BoxDecoration(
        color: HexColor("ff0000"),
        gradient: LinearGradient(
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
          colors: [HexColor('ff9874'), HexColor('fe5d68')],
        ));
  }
  //style:E

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          color: HexColor("ffffff"),
        ),
        child: Column(
          children: <Widget>[
            //标题栏
            Container(
                decoration: _boxDecorationOfAppBar(),
                width: double.infinity,
                height: Design.px(132),
                child: appbar),
            //正文内容
            Expanded(
              child: Container(
                decoration: BoxDecoration(color: HexColor("f8f8f8")),
                width: double.infinity,
                child: body,
              ),
            )
          ],
        ),
      ),
    );
  }
}

class PageAppBar extends StatelessWidget {
  final String title;
  final Widget action;
  final VoidCallback doAction;

  const PageAppBar(this.title, {Key key, this.action, this.doAction})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        children: <Widget>[
          Padding(
            padding: EdgeInsets.symmetric(horizontal: Design.px(25)),
            child: FlatButton(
              onPressed: () => {Navigator.pop(context)},
              child: Image.asset('assets/icon-back.png', height: Design.px(50),),
            ),
          ),
          Expanded(
            child: Padding(
                padding: EdgeInsets.only(left: Design.px(69)),
                child: Container(
                  alignment: Alignment.center,
                  child: Text(title, style: XStyles.navBarTitle()),
                )),
          ),
          action != null
              ? Padding(
                  padding: EdgeInsets.symmetric(horizontal: Design.px(25)),
                  child: FlatButton(
                    onPressed: doAction,
                    child: action,
                  ),
                )
              : SizedBox.shrink(),
        ],
      ),
    );
  }
}

class HomepageAppBar extends StatelessWidget {
  final String title;
  final String avatar;
  final String nickname;

  const HomepageAppBar({Key key, this.title, this.avatar, this.nickname})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        children: <Widget>[
          Expanded(
            child: Padding(
              padding: EdgeInsets.only(left: Design.px(69)),
              child: Text(title, style: XStyles.navBarTitle()),
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: Design.px(25)),
            child: Row(
              children: <Widget>[
                AppBarAvatar(avatar),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: Design.px(20)),
                  child:
                      Text(nickname, style: XStyles.navBarNickNameTextStyle()),
                ),
                AppBarFlatButton(
                  text: '管理',
                ),
                AppBarFlatButton(
                  text: '交班',
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class AppBarAvatar extends StatelessWidget {
  final String url;
  const AppBarAvatar(this.url, {Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ClipOval(
      child: FadeInImage.assetNetwork(
        placeholder: "assets/logo.png", //预览图
        fit: BoxFit.cover,
        image: url,
        width: Design.px(80),
        height: Design.px(80),
      ),
    );
  }
}

class AppBarFlatButton extends StatelessWidget {
  final String text;
  const AppBarFlatButton({Key key, this.text}) : super(key: key);

  static final BorderRadius _roundRadius = BorderRadius.circular(Design.px(58));

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(
          horizontal: Design.px(8), vertical: Design.px(5)),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.white, width: Design.px(1)),
        borderRadius: _roundRadius,
      ),
      child: FlatButton(
          onPressed: () => {},
          shape: RoundedRectangleBorder(borderRadius: _roundRadius),
          child: Text(
            text,
            style: XStyles.navBarFlatButtonTextStyle(),
          ),
          padding: EdgeInsets.symmetric(
              horizontal: Design.px(8), vertical: Design.px(5))),
    );
  }
}
